<?php

// +--------------------------------------------
// | Name： 星空后台首页
// +--------------------------------------------
// | Version: V5.0
// +--------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +--------------------------------------------
// | Time: 2021-04-08 09:13
// +--------------------------------------------
declare(strict_types=1);

namespace app\xingkong\controller\v1;

use app\common\common\BackendController;
use app\common\services\AuthService;
use app\common\model\User as UserModel;
use app\common\model\UserLevel as UserLevelModel;
use app\common\model\SubOrder as SubOrderModel;
use app\common\model\GoodsOrder as GoodsOrderModel;
use app\common\model\RefundOrder as RefundOrderModel;
use app\common\model\Finance as FinanceModel;
use app\common\model\Goods as GoodsModel;
use think\facade\View;

/**
 * @xk 首页
 */
class Index extends BackendController
{
    /**
     * @xk 首页入口
     *
     * @method index
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-08T09:13:48+080
     */
    public function index()
    {
        return View::fetch();
    }

    /**
     * @xk 左侧菜单获取
     *
     * @method getLeftMenu
     *
     * @param AuthService $authService [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-20T18:33:15+080
     */
    public function getLeftMenu(AuthService $authService)
    {
        $leftMenuArray = [];
        $leftMenuArray['homeInfo'] = [
          'title' => '控制台',
          'href' => '/'.config('manage.manage_name').'/'.config('manage.version').'/index/welcome',
      ];
        $leftMenuArray['logoInfo'] = [
          'title' => '',
          'image' => config('web_config.web_icon'),
          'href' => '',
      ];
        $leftMenu = $authService->getSystemMenu();
        $leftMenuArray['menuInfo'] = $leftMenu;
        $code = 200;
        $msg = '请求成功';
        $result_code = 200;
        $result_msg = '加载成功';
        $resultData = [];
        $resultData['list'] = $leftMenuArray;
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }

    /**
     * @xk 欢迎主体
     *
     * @method  welcome
     *
     * @return [type] [description]
     * @Author: xkyinzhe                <zhonghuaxinxing@sian.cn>
     * @Time:   2021-04-14T15:09:53+080
     */
    public function welcome(UserModel $userModel, SubOrderModel $subOrderModel, RefundOrderModel $refundOrderModel, FinanceModel $financeModel)
    {
        // $now_day = date('Y-m-d'); // 当日
        // $now_month = date('Y-m'); // 当月
        // $month_condition = [];
        // $day_condition = [];
        // $month_condition[] = ['create_time','like',$now_month.'%'];
        // $day_condition[] = ['create_time','like',$now_day.'%'];

        // 订单数据统计
        //  待付款
        $all_wait_pay = $subOrderModel->where([['status','=',0]])->whereTime('create_time', 'today')->count();
        // 待发货
        $all_wait_shipping = $subOrderModel->where([['status','=',1]])->whereTime('create_time', 'today')->count();
        // 待退款
        $all_wait_back = $refundOrderModel->where([['status','=',0]])->whereTime('create_time', 'today')->count();
        // 已退款
        $all_over_back = $refundOrderModel->where([['status','in','1.3,2.5,3.4']])->whereTime('create_time', 'today')->count();
        $all_year_get = $financeModel->whereTime('create_time', 'year')->where([['entry','=',1]])->sum('money');
        $all_year_back_get = $financeModel->whereTime('create_time', 'year')->where([['entry','=',2]])->sum('money');
        // $all_year_get = $all_year_get-$all_year_back_get;

        // 会员统计
        $all_user = $userModel->count(); // 会员总量
        $all_day_user = $userModel->whereTime('create_time', 'today')->count(); // 今日新增
        $all_month_user = $userModel->whereTime('create_time', 'month')->count(); // 当月新增
        $all_week_user = $userModel->whereTime('create_time', 'week')->count();
        // 获取下单会员总量
        $buy_order_condition = [];
        $buy_order_condition[] = ['status','in','1,2,3,4,5'];
        $buy_order_condition[] = ['pay_status','=',1];
        $buy_order_condition[] = ['refund_status','<>',2];
        $all_buy_order_count = $subOrderModel->where($buy_order_condition)->group('user_sn')->count();
        $all_buy_day_order_count = $subOrderModel->where($buy_order_condition)->whereTime('create_time', 'today')->group('user_sn')->count();
        $all_buy_week_order_count = $subOrderModel->where($buy_order_condition)->whereTime('create_time', 'week')->group('user_sn')->count();
        $all_buy_month_order_count = $subOrderModel->where($buy_order_condition)->whereTime('create_time', 'month')->group('user_sn')->count();
        // 获取订单统计
        $all_order = $subOrderModel->where($buy_order_condition)->count();
        $all_day_order = $subOrderModel->where($buy_order_condition)->whereTime('create_time', 'today')->count();
        $all_week_order = $subOrderModel->where($buy_order_condition)->whereTime('create_time', 'week')->count();
        $all_month_order = $subOrderModel->where($buy_order_condition)->whereTime('create_time', 'month')->count();


        // // 订单统计
        // $sub_order_sn_arr = $refundOrderModel->where([['status','=',0]])->column('sub_order_sn');
        // $order_condition = $order_condition1 = [];
        // $order_day_condition = $order_day_condition1 = $day_condition;
        // $order_month_condition = $order_month_condition1 = $month_condition;
        // $order_month_condition[] = $order_month_condition1[] = $order_day_condition[] = $order_day_condition1[] = $order_condition[] = $order_condition1[] = ['status','<>',6];
        // $order_month_condition[] = $order_month_condition1[] = $order_day_condition[] = $order_day_condition1[] = $order_condition[] = $order_condition1[] = ['pay_status','=',1];
        // $order_month_condition[] = $order_day_condition[] = $order_condition[] = ['refund_status','<>',2];
        // $order_month_condition1[] = $order_day_condition1[] = $order_condition1[] = ['refund_status','=',2];
        // $order_month_condition1[] = $order_day_condition1[] = $order_condition1[] = ['sub_order_sn','in',$sub_order_sn_arr];
        //
        //
        // $all_order = $subOrderModel->whereOr([$order_condition,$order_condition1])->count();
        // $all_day_order = $subOrderModel->whereOr([$order_day_condition,$order_day_condition1])->count();
        // $all_month_order = $subOrderModel->whereOr([$order_month_condition,$order_month_condition1])->count();
        // // 售后统计
        $refund_order_condition= [];
        $refund_order_condition[] = ['status','<>','6.1'];
        $all_refund_order = $refundOrderModel->where($refund_order_condition)->count();
        $all_day_refund_order = $refundOrderModel->where($refund_order_condition)->whereTime('create_time', 'today')->count();
        $all_week_refund_order = $refundOrderModel->where($refund_order_condition)->whereTime('create_time', 'week')->count();
        $all_month_refund_order = $refundOrderModel->where($refund_order_condition)->whereTime('create_time', 'month')->count();
        // // 订单总收入
        $all_price = $financeModel->where([['entry','=',1]])->sum('money');
        $all_day_price = $financeModel->where([['entry','=',1]])->whereTime('create_time', 'today')->sum('money');
        $all_week_price = $financeModel->where([['entry','=',1]])->whereTime('create_time', 'week')->sum('money');
        $all_month_price = $financeModel->where([['entry','=',1]])->whereTime('create_time', 'month')->sum('money');
        $order_condition = [];
        $order_condition[] = ['pay_status','=',1];
        // 运费总收入
        $all_freight = $subOrderModel->where($order_condition)->sum('total_freight');
        $all_day_freight = $subOrderModel->where($order_condition)->whereTime('create_time', 'today')->sum('total_freight');
        $all_week_freight = $subOrderModel->where($order_condition)->whereTime('create_time', 'week')->sum('total_freight');
        $all_month_freight = $subOrderModel->where($order_condition)->whereTime('create_time', 'month')->sum('total_freight');
        // // 售后 退款总金额
        $finance_condition = [];
        $finance_condition[] = ['entry','=',2];
        $finance_condition[] = ['act','=',4];
        $all_refund_price = $financeModel->where($finance_condition)->sum('money');
        $all_day_refund_price = $financeModel->where($finance_condition)->whereTime('create_time', 'today')->sum('money');
        $all_week_refund_price = $financeModel->where($finance_condition)->whereTime('create_time', 'week')->sum('money');
        $all_month_refund_price = $financeModel->where($finance_condition)->whereTime('create_time', 'month')->sum('money');

        return View::fetch('', [
        'all_user'=>$all_user,
        'all_day_user'=>$all_day_user,
        'all_month_user'=>$all_month_user,
        'all_week_user' => $all_week_user,
        'all_buy_order_count' => $all_buy_order_count,
        'all_buy_day_order_count' => $all_buy_day_order_count,
        'all_buy_month_order_count' => $all_buy_month_order_count,
        'all_buy_week_order_count' => $all_buy_week_order_count,
        'all_order' => $all_order,
        'all_day_order' => $all_day_order,
        'all_month_order' => $all_month_order,
        'all_week_order' => $all_week_order,
        'all_refund_order' => $all_refund_order,
        'all_day_refund_order' => $all_day_refund_order,
        'all_week_refund_order' => $all_week_refund_order,
        'all_month_refund_order' => $all_month_refund_order,
        'all_price' => $all_price-$all_refund_price,
        'all_day_price' => $all_day_price-$all_day_refund_price,
        'all_week_price' => $all_week_price-$all_week_refund_price,
        'all_month_price' => $all_month_price-$all_month_refund_price,
        'all_freight' => $all_freight,
        'all_day_freight' => $all_day_freight,
        'all_week_freight' => $all_week_freight,
        'all_month_freight' => $all_month_freight,
        'all_refund_price' => $all_refund_price,
        'all_day_refund_price' => $all_day_refund_price,
        'all_week_refund_price' => $all_week_refund_price,
        'all_month_refund_price' => $all_month_refund_price,
        'all_wait_pay' => $all_wait_pay,
        'all_wait_shipping' => $all_wait_shipping,
        'all_wait_back' => $all_wait_back,
        'all_over_back' => $all_over_back,
        'all_year_get' => $all_year_get,
        'all_year_back_get' => $all_year_back_get
      ]);
    }
    /**
     * @xk 获取会员数据
     *
     * @method getUserCount
     * @param  UserModel     $userModel                 [description]
     * @param  UserCardModel $userCardModel             [description]
     * @return [type]        [description]
     * @Time   2021-11-12
     * @City   青岛
     * @Author xkyinzhe      <zhonghuaxinxing@sina.cn>
     */
    public function getUserCount(UserModel $userModel, UserLevelModel $UserLevelModel)
    {
        $nowTime = time();
        $level_list = $UserLevelModel->field('`level_name`,`level`')->select()->toArray();
        // 获取当前月所有天数
      $first_day = strtotime(date('Y-m-01')); // 获取当月第一天
      $dayarray = [];
        $nowDay = date('d');
        for ($i = 1; $i <= $nowDay; ++$i) {
            if ($i < 10) {
                $dayarray[] = '0'.$i.'号';
            } else {
                $dayarray[] = $i.'号';
            }
        }
        $dataArray = [];
        foreach ($level_list as $key => $value) {
            $newArr = [];
            $newArr['name'] = $value['level_name'];
            $newArr['type'] = 'line';
            $newArr['areaStyle'] = [];
            $condition = [];
            $condition[] = ['level', '=', $value['level']];
            $newCountArray = [];
            for ($i = 1; $i <= $nowDay; ++$i) {
                if ($i < 10) {
                    $day = date('Y-m-').'0'.$i;
                } else {
                    $day = date('Y-m-').$i;
                }
                $conditionx = [];
                $conditionx[] = ['create_time', 'like', $day.'%'];
                $newCountArray[] = $userModel->where($conditionx)->where($condition)->count();
            }
            $newArr['data'] = $newCountArray;
            $dataArray[$key] = $newArr;
        }

        $name = array_column($level_list, 'level_name');

        $resultData['name'] = $name;
        $resultData['dayarray'] = $dayarray;
        $resultData['dataArray'] = $dataArray;
        $this->result(200, '请求成功', 200, '加载成功', $resultData);
    }
    /**
     * @xk 获取订单数据统计
     *
     * @method getOrderCount
     * @param  SubOrderModel $subOrderModel             [description]
     * @return [type]        [description]
     * @Time   2021-11-12
     * @City   青岛
     * @Author xkyinzhe      <zhonghuaxinxing@sina.cn>
     */
    public function getOrderCount(SubOrderModel $subOrderModel)
    {
        $nowTime = time();
        $order_status = [
          '0' => '等待付款',
          '1' => '等待发货',
          '2' => '待收货',
          '3' => '已完成',
          '6' => '已取消',
          '7' => '售后'
        ];
        // 获取当前月所有天数
        $first_day = strtotime(date('Y-m-01')); // 获取当月第一天
        $dayarray = [];
        $nowDay = date('d');
        for ($i = 1; $i <= $nowDay; ++$i) {
            if ($i < 10) {
                $dayarray[] = '0'.$i.'号';
            } else {
                $dayarray[] = $i.'号';
            }
        }
        $dataArray = [];
        foreach ($order_status as $key => $value) {
            $newArr = [];
            $newArr['name'] = $value;
            $newArr['type'] = 'line';
            $newArr['areaStyle'] = [];
            $condition = [];
            if ($key < 7) {
                $condition[] = ['status', '=', $key];
                $condition[] = ['refund_status','=',0];
            } else {
                $condition[] = ['refund_status','<>',0];
            }
            $newCountArray = [];
            for ($i = 1; $i <= $nowDay; ++$i) {
                if ($i < 10) {
                    $day = date('Y-m-').'0'.$i;
                } else {
                    $day = date('Y-m-').$i;
                }
                $conditionx = [];

                $conditionx[] = ['create_time', 'like', $day.'%'];
                $newCountArray[] = $subOrderModel->where($conditionx)->where($condition)->count();
            }
            $newArr['data'] = $newCountArray;
            $dataArray[] = $newArr;
        }
        $resultData['name'] = array_values($order_status);
        $resultData['dayarray'] = $dayarray;
        $resultData['dataArray'] = $dataArray;
        $this->result(200, '请求成功', 200, '加载成功', $resultData);
    }
    /**
     * @xk 获取产品销量统计
     *
     * @method getGoodsCount
     * @param  GoodsModel    $goodsModel                [description]
     * @return [type]        [description]
     * @Time   2021-11-12
     * @City   青岛
     * @Author xkyinzhe      <zhonghuaxinxing@sina.cn>
     */
    public function getGoodsCount(GoodsOrderModel $goodsOrderModel)
    {
        // 开始统计产品销售额
        $condition = [];
        $condition[] = ['go.status','in','3.1,4.1'];
        $goods_list_count = $goodsOrderModel
                            ->alias('go')
                            ->leftJoin('goods g','g.goods_id = go.goods_id')
                            ->where($condition)
                            ->field('g.`goods_name`,sum(go.`sub_total`) as goods_price')
                            ->order(['goods_price desc'])
                            ->group('goods_id')
                            ->select()
                            ->toArray();


        // $goods_list_count = $goodsModel
        //                   ->alias('g')
        //                   ->field('g.`goods_name`,g.`shell_num`,g.`stock`')
        //                   ->withAttr('shell_num', function ($value, $data) {
        //                       if ($value > 0) {
        //                           return $value;
        //                       } else {
        //                           return 0;
        //                       }
        //                   })
        //                   ->group('g.goods_name')
        //                   ->select()
        //                   ->toArray();
        $name = ['product','销售额'];
        $sitypes = [['type'=>'bar']];
        $dayarray = [];
        foreach ($goods_list_count as $key => $value) {
            $dayarray[$key]['product'] = $value['goods_name'];
            $dayarray[$key]['销售额'] = $value['goods_price'];
            // $dayarray[$key]['库存'] = $value['stock'];
        }
        // dump($dayarray); die();
        $resultData['name'] = $name;
        $resultData['dayarray'] = $dayarray;
        $resultData['sitypes'] = $sitypes;
        $this->result(200, '请求成功', 200, '加载成功', $resultData);
    }
    /**
     * @xk 获取财务统计
     *
     * @method getFinanceCount
     * @param  FinanceModel    $financeModel              [description]
     * @return [type]          [description]
     * @Time   2021-11-12
     * @City   青岛
     * @Author xkyinzhe        <zhonghuaxinxing@sina.cn>
     */
    public function getFinanceCount(FinanceModel $financeModel)
    {
        $nowTime = time();
        $name = ['入账','出账'];
        // 获取当前月所有天数
      $first_day = strtotime(date('Y-m-01')); // 获取当月第一天
      $dayarray = [];
        $nowDay = date('d');
        for ($i = 1; $i <= $nowDay; ++$i) {
            if ($i < 10) {
                $dayarray[] = '0'.$i.'号';
            } else {
                $dayarray[] = $i.'号';
            }
        }
        $dataArray = [];
        foreach ($name as $key => $value) {
            $newArr = [];
            $newArr['name'] = $value;
            $newArr['type'] = 'line';
            $newArr['areaStyle'] = [];
            $condition = [];
            switch ($value) {
            case '入账':
              $condition[] = ['entry','=',1];
              break;
            case '出账':
              $condition[] = ['entry','=',2];
              break;
          }
            $newCountArray = [];
            for ($i = 1; $i <= $nowDay; ++$i) {
                if ($i < 10) {
                    $day = date('Y-m-').'0'.$i;
                } else {
                    $day = date('Y-m-').$i;
                }
                $conditionx = [];
                $conditionx[] = ['create_time', 'like', $day.'%'];
                $newCountArray[] = $financeModel->where($conditionx)->where($condition)->sum('money');
            }
            $newArr['data'] = $newCountArray;
            $dataArray[] = $newArr;
        }
        $resultData['name'] = $name;
        $resultData['dayarray'] = $dayarray;
        $resultData['dataArray'] = $dataArray;
        $this->result(200, '请求成功', 200, '加载成功', $resultData);
    }
}
